package com.netty.esc.service.http.impl;

import com.netty.esc.common.enums.UserImKeyEnum;
import com.netty.esc.common.global.UserCache;
import com.netty.esc.entity.model.PersonTalkCountDTO;
import com.netty.esc.entity.pojo.UserInfo;
import com.netty.esc.service.http.ConversationService;
import com.netty.esc.entity.model.PersonTalkDTO;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ConversationServiceImpl
 * @Description:
 * @Author: ZY
 * @Date: 2022/12/23 22:09
 **/
@Service
public class ConversationServiceImpl implements ConversationService {

    @Resource
    RedisTemplate<String,Object> redisTemplate;
    @Resource
    MongoTemplate mongoTemplate;

    @Override
    public List<PersonTalkCountDTO> findHistoryNum(UserInfo currentUser) {
        List<PersonTalkCountDTO> personTalkCountDTOS = new ArrayList<>();
        Long id = currentUser.getId();
        Map<Object, Object> entries = redisTemplate.opsForHash().entries(UserImKeyEnum.ESC_IM_OFF_ONLINE_MESSAGE.getKey() + ":" + id);
        entries.forEach((k ,v)->{
            Long userId = Long.valueOf((String) k);
            Date time = (Date) v;
            Criteria criteria = Criteria.where("createTime").gt(time).orOperator(Criteria.where("userId").is(userId).and("receiverId").is(id),Criteria.where("userId").is(id).and("receiverId").is(userId));
//            Criteria criteria = new Criteria();
//            criteria.orOperator(Criteria.where("userId").is(userId).and("receiverId").is(id),Criteria.where("userId").is(id).and("receiverId").is(userId));
            Query query = new Query(criteria);
            long count = mongoTemplate.count(query, PersonTalkDTO.class);
//            List<PersonTalkDTO> personTalkDTOS1 = mongoTemplate.find(query, PersonTalkDTO.class);
            query.with(Sort.by(Sort.Order.desc("createTime")));
            query.limit(1);
            List<PersonTalkDTO> personTalkDTOS = mongoTemplate.find(query, PersonTalkDTO.class);
            PersonTalkDTO personTalkDTO = personTalkDTOS.get(0);
            PersonTalkCountDTO personTalkCountDTO = new PersonTalkCountDTO();
            personTalkCountDTO.setNum(count);
            personTalkCountDTO.setPersonTalkDTO(personTalkDTO);
            personTalkCountDTOS.add(personTalkCountDTO);
        });
        redisTemplate.delete(UserImKeyEnum.ESC_IM_OFF_ONLINE_MESSAGE.getKey() + ":" + id);
        return personTalkCountDTOS;
    }


    @Override
    public List<PersonTalkDTO> findHistory(Integer page, Integer size, Long sendId) {
        UserInfo currentUser = UserCache.getCurrentUser();
        Criteria criteria = new Criteria();
        criteria.orOperator(Criteria.where("userId").is(currentUser.getId()).and("receiverId").is(sendId),Criteria.where("userId").is(sendId).and("receiverId").is(currentUser.getId()));
        Query query = new Query(criteria);
        int skip =  page*size;
        query.with(Sort.by(Sort.Order.desc("createTime"))).limit(size).skip(skip);
        return mongoTemplate.find(query, PersonTalkDTO.class);
    }
}
